<template>
  <div>
    <div class="wrapper">
      <div style="height: 0px;opacity: 0.8" >
        <h2 style="color: #E6A23C;position:relative;right: -70px;bottom: -8px;width: 220px;height: 42px;line-height: 42px">IKUN后台管理系统</h2>
        <img src="../assets/logo.png" alt="" width="57" style="position: relative;top: -35px;right:-13px;opacity: 0.8" >
      </div>
      <div id="building">
        <div style="text-align: center;user-select: none;
      letter-spacing: 2px;padding: 14px 0;">
          <h1 style="color: #1E9FFF;font-weight: bold;font-size: 25px">用户登录</h1></div>
        <el-form :model="user" :rules="rules" ref="userForm">
          <el-form-item prop="username" >
            <el-input  size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password" >
            <el-input size="medium" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
          </el-form-item>
        </el-form>

        <div style="margin: 10px 0; text-align: right">
          <el-button type="success" size="large" autocomplete="off" @click="login">登录</el-button>
          <el-button type="warning" size="large" autocomplete="off" @click="$router.push('/register')">注册</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {setRouters} from "@/router";

export default {
  name: "Login",
  data() {
    return {
      user:{},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: "长度在3到10个字符", trigger: "blur"},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {  type: 'string', message: '请输入不包含空格的字符', trigger: 'blur', transform(value) {
              if (value && value.indexOf(' ') === -1) {
                return value
              } else {
                return false
              }
            }
          },]
      }
    }
  },
  methods:{
    login(){
      this.$refs['userForm'].validate((valid) =>{
        if(valid){        //表单校验
          this.request.post("/user/login",this.user).then(res=>{
            if(res.code=='200'){
              localStorage.setItem("user",JSON.stringify(res.data))     //存储用户信息到浏览器
              localStorage.setItem("menus",JSON.stringify(res.data.menus))     //存储用户信息到浏览器
              //动态设置当前路由
              setRouters()
              this.$message.success("登录成功")
              this.$router.push("/home");
            }else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped >
.wrapper{
  background-size: cover;
  height: 100vh;
  overflow: hidden;
  background-image: url("../assets/bg.jpg");
}
#particles-js {
  width: 100%;
  position: absolute;
}
#building{
  position: relative;
  margin: 200px auto;
  background-color:#fff;
  width: 350px;
  height: 300px;
  padding: 20px;
  border-radius: 10px;
  /*为其整体设置接近透明的效果*/
  background-color: rgba(255,255,255,0.4);
  /*设置box更加有立体感*/
  box-shadow: 5px 5px 0 0 rgba(0,0,0,0.2);
}
</style>